class_name FollowTargetComponent_Weapon
extends BaseComponent

#region 属性
var weapon:Weapon
var weapon_data:EntityData_Weapon
var connect_obj_dict:Dictionary = {}
var enable:bool = false
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
func init_component(args:Dictionary = {}):
	super(args)
	self.weapon = args.get("Weapon")
	self.weapon_data = self.weapon.get_weapon_data()
	connect_obj_dict["WeaponStateChanged"] = ConnectObj.new(weapon.weapon_state_changed,[on_weapon_state_changed])
	inited = true
func obtain(entity:Entity):
	pass
func discard():
	pass
func restore_pickable():
	pass
func equip():
	set_enable(true)
func unequip():
	set_enable(false)
func destroy():
	for connect_obj:ConnectObj in connect_obj_dict.values():
		connect_obj.disconnect_all()
func on_weapon_state_changed(last_weapon_state:GameEnum.WeaponState,cur_weapon_state:GameEnum.WeaponState):
	match cur_weapon_state:
		GameEnum.WeaponState.UnPickable:
			discard()
		GameEnum.WeaponState.Pickable:
			restore_pickable()
		GameEnum.WeaponState.Obtain:
			obtain(weapon.get_weapon_entity())
		GameEnum.WeaponState.UnEquip:
			unequip()
		GameEnum.WeaponState.Equip:
			equip()
		GameEnum.WeaponState.Destroy:
			destroy()
		_:
			pass
func get_connect_obj_dict() -> Dictionary:
	return connect_obj_dict
func set_enable(enable):
	if self.enable != enable:
		self.enable = enable
func follow(delta:float):
	var detector_component:DetectComponent = entity.get_component(DetectComponent)
	if not detector_component:
		return
	var detector_selection:Detector_Selection = detector_component.get_detector(GameEnum.DetectorType.Selection)
	if not detector_selection:
		return
	detector_selection.update(delta)
	var target = detector_selection.get_selection()
	if not target:
		return
	if typeof(target) == TYPE_VECTOR2:
		weapon.look_at(target)
		var flag = 1
		var weapon_slot:Marker2D = entity.get_component(SlotComponent).get_slot_manager().get_slot_by_type(GameEnum.SlotType.Weapon)
		if weapon_slot.transform.x.dot(weapon.transform.x) >= 0:
			flag = (-1 if weapon_slot.global_transform.x.x >= 0 else 1) if weapon.transform.x.x * weapon.global_scale.y < 0 else (1 if weapon_slot.global_transform.x.x >= 0 else -1)
		else:
			flag = (1 if weapon_slot.global_transform.x.x >= 0 else -1) if weapon.transform.x.x * weapon.global_scale.y < 0 else (-1 if weapon_slot.global_transform.x.x >= 0 else 1)
		weapon.global_scale = Vector2(weapon.global_scale.x,flag * abs(weapon.global_scale.y))
	elif typeof(target) == TYPE_OBJECT and target is Node2D:
		weapon.look_at(target.global_position)
		var flag = 1
		var weapon_slot:Marker2D = entity.get_component(SlotComponent).get_slot_manager().get_slot_by_type(GameEnum.SlotType.Weapon)
		if weapon_slot.transform.x.dot(weapon.transform.x) >= 0:
			flag = (-1 if weapon_slot.global_transform.x.x >= 0 else 1) if weapon.transform.x.x * weapon.global_scale.y < 0 else (1 if weapon_slot.global_transform.x.x >= 0 else -1)
		else:
			flag = (1 if weapon_slot.global_transform.x.x >= 0 else -1) if weapon.transform.x.x * weapon.global_scale.y < 0 else (-1 if weapon_slot.global_transform.x.x >= 0 else 1)
		weapon.global_scale = Vector2(weapon.global_scale.x,flag * abs(weapon.global_scale.y))
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _process(delta: float) -> void:
	if not enable:
		return 
	follow(delta)
#endregion
